<?php

namespace app\adminapi\controller\v1\distributor;

use app\adminapi\controller\AuthController;
use app\adminapi\export\DistributorExport;
use app\Request;
use app\services\distributor\DistributorService;
use think\facade\App;

class Distributor extends AuthController
{

    protected $service;

    public function __construct(App $app, DistributorService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    public function index()
    {
        $where = $this->request->getMore([
            ['distributor_name', ''], // 经销商名称
            ['add_time', []], // 添加时间
            ['page', 1],
            ['limit', 15]
        ]);
        $data = $this->service->getList($where);
        return app('json')->success($data);
    }

    public function export(){
        $where = $this->request->getMore([
            ['distributor_name', ''], // 经销商名称
            ['add_time', []], // 添加时间
            ['page', 1],
            ['limit', 15]
        ]);
        return app()->make(DistributorExport::class)->export($where);
    }

    public function save($id)
    {
        $data = $this->request->postMore([
            ['distributor_name', ''],// 经销商名称
            ['contact_person', ''],// 联系人
            ['contact_info', ''], // 联系方式
            ['responsible_area', ''], // 负责区域
            ['status', 1],
        ]);
        $this->service->save((int)$id, $data);
        return app('json')->success(100000);
    }

    /**
     * 批量开启经销商
     * @return mixed
     */
    public function distributor_show()
    {
        [$ids] = $this->request->postMore([
            ['ids', []]
        ], true);
        $this->service->setStatus($ids, 1);
        return app('json')->success(100014);
    }

    /**
     * 批量禁用经销商
     * @return mixed
     */
    public function distributor_unshow()
    {
        [$ids] = $this->request->postMore([
            ['ids', []]
        ], true);
        $this->service->setStatus($ids, 0);
        return app('json')->success(100014);
    }

    public function audit_list()
    {
        $where = $this->request->getMore([
            ['distributor_name', ''], // 经销商名称
            ['add_time', []] // 添加时间
        ]);
        $data = $this->service->getAuditList($where);
        return app('json')->success($data);
    }

    public function all()
    {
        $list = $this->service->all();
        return app('json')->success($list);
    }

}